﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.DemandPlan
{
    public partial class BidPlanBusiness : System.Web.UI.Page
    {
        Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                    if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                    {
                        int dpid = (Convert.ToInt32(Request.Params["id"]));

                        Bind(person_id,dpid);
                    }
                   
                }
            }
        }

        //添加供应商
        protected void btn_adds_Click(object sender, EventArgs e)
        {
            string sup = this.txt_supplier.Text;

            if (!string.IsNullOrEmpty(sup))
            {
                string [] supr = sup.Split('|');
                string sname = supr[0];
                string sid = supr[1];

                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                    Maticsoft.BLL.supplier_bidplan sbm = new Maticsoft.BLL.supplier_bidplan();

                    Maticsoft.Model.supplier_bidplan sb = new Maticsoft.Model.supplier_bidplan();

                    sb.submit_user_id = person_id;
                    sb.subTime = DateTime.Now;
                    sb.supplier_id = int.Parse(sid);

                    if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                    {
                        int dpid = (Convert.ToInt32(Request.Params["id"]));
                        List<int> nextId = lcs.BLL.Tools.GetNextFlowUserId(person_id, dpid);//获取财务确认人

                        if (nextId.Count == 1)
                        {
                            sb.confirmBidFee_user_id = nextId[0];//标书费确认财务
                            sb.confirmBidDeposit_user_id = nextId[0];//保证金确认财务
                        }
                  

                    }
                 
                    if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                    {
                        int dpid = (Convert.ToInt32(Request.Params["id"]));

                        sb.dpid = dpid;

                        if (sbm.Add(sb) > 0)
                        {
                            Bind(person_id, dpid);
                        }
                    }

                    
                }
            }
        }


        private void Bind(int uid,int dpid)
        {
            DataSet ds = new Maticsoft.BLL.supplier_bidplan().GetListByView(" submit_user_id =  "+uid +"  and dpid="+dpid);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();



        }
        //提交财务确认
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                int dpid = Convert.ToInt32(Request["id"]);

                List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

                if (li.Count == 1)
                {
                    Maticsoft.Model.documentFlowInfo d = li[0];

                    string reason = "同意";

                    int person_id = 0;
                    if (Session["login"] != null)
                    {
                        DataSet ds = Session["login"] as DataSet;

                        person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                    }

                    //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                    int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                    int flow_node_id_now = 0;

                    flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                    if (wm.UpdateW(person_id, d.document_id.Value, 1, reason, DateTime.Now, flow_node_id_now, flow_node_num_now))//修改本身意见为同意,添加处理意见、处理时间
                    {
                        int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now),d.flow_id.Value);

                        string str = wm.Submit_judge(flow_node_id, person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now));//得到审核状态

                        if (str == "同意")//1、如果是会签非尾节点同意，插入下一节点
                        {
                            lcs.BLL.Tools.DealWorkFlow(d, flow_node_id, person_id);

                        }

                        if (str == "尾节点同意")//2、如果是会签尾节点同意
                        {
                            lcs.BLL.Tools.DealLast(d, person_id);

                        }

                        if (str == "非会签")//3、如果是非会签非尾节点，插入下一结点
                        {
                            lcs.BLL.Tools.DealWorkFlow(d, flow_node_id, person_id);
                        }

                        if (str == "尾节点非会签")
                        {
                            lcs.BLL.Tools.DealLast(d, person_id);
                        }
                    }

                    Response.Redirect("ListBidPlanBusiness.aspx");
                }

            }
        }

       
    }
}